package com.zzsy.carosshopprojects.activitys.login.fragment;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.login.vmode.LoginMode;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.util.SPUtil;

import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录
 */
public class LoginFragment extends BaseFragment {
    public static LoginFragment newInstance() {
        LoginFragment loginFragment = new LoginFragment();
        return loginFragment;
    }

    @BindView(R.id.et_user)
    EditText etAccount;//账号输入框
    @BindView(R.id.et_psd)
    EditText etPsd;//密码输入框
    @BindView(R.id.cb_check)
    ImageView cbPsd;//是否记住账号密码
    @BindView(R.id.btn_login)
    TextView tv_login;//登录
    @BindDimen(R.dimen.p_180)
    int p_180;//获取尺寸大小
    private LoginMode loginMode;
    private boolean isRember;//用做标记是否记住密码

    @Override
    protected int initLayout() {
        return R.layout.fg_login;
    }

    @Override
    protected void initView(View view) {
        //tv_login.setBackground(ViewBgUtil.setBg(p_180, Color.parseColor(ConfigureFile.ColorSystem)));
    }

    @Override
    protected void initData(Context mContext) {
        loginMode = getViewModelProvider().get(LoginMode.class);
    }

    @Override
    protected void loadMethod() {
        showData();
    }

    @OnClick({R.id.btn_login, R.id.ly_state})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                LoginMode.builder().setIsFinis(false)
                        .setState(isRember == true ? LoginMode.SAVE : LoginMode.NOTSAVE)
                        .login((RxAppCompatActivity) getActivity(),getPsd(), getAccount());
                break;
            case R.id.ly_state:
                if (isRember) {
                    isRember = false;
                    cbPsd.setImageResource(R.mipmap.icon_selectno);
                } else {
                    isRember = true;
                    cbPsd.setImageResource(R.mipmap.icon_selectyes);
                }
                SPUtil.getInstance().putBoolean(ConfigureFile.ISREMENBER, isRember);
                break;
        }
    }

    private String getAccount() {
        return etAccount.getText().toString();
    }

    private String getPsd() {
        return etPsd.getText().toString();
    }

    private void showData() {
        loginMode.getLive().observe(this, showDataBean -> {
            if (showDataBean.isRemenber()) {
                etPsd.setText(showDataBean.getPwd());
                cbPsd.setImageResource(R.mipmap.icon_selectyes);
                isRember = true;
            } else {
                cbPsd.setImageResource(R.mipmap.icon_selectno);
                isRember = false;
            }
            etAccount.setText(showDataBean.getAccount());
        });
        loginMode.postShowData();
    }
}
